package com.hhtc.jsj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhtc.jsj.common.utils.ShiroUtil;
import com.hhtc.jsj.entity.Post;
import com.hhtc.jsj.entity.PostComment;
import com.hhtc.jsj.entity.User;
import com.hhtc.jsj.mapper.PostCommentMapper;
import com.hhtc.jsj.request.PostCommentRequest;
import com.hhtc.jsj.request.UserRequest;
import com.hhtc.jsj.service.PostCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhtc.jsj.service.PostService;
import com.hhtc.jsj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author ty
 * @since 2022-04-07
 */
@Slf4j
@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements PostCommentService {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    @Override
    @Transactional
    public boolean add(PostCommentRequest postCommentRequest) {
        postCommentRequest.setId(IdWorker.getId());
        postCommentRequest.setCommentUserId(ShiroUtil.getProfile().getId());
        postCommentRequest.setCreatedTime(System.currentTimeMillis());
        postCommentRequest.setCreatedBy(ShiroUtil.getProfile().getId().toString());
        postCommentRequest.setUpdatedTime(System.currentTimeMillis());
        postCommentRequest.setUpdatedBy(ShiroUtil.getProfile().getId().toString());
        PostComment postComment = BeanUtil.copyProperties(postCommentRequest, PostComment.class);
        return this.save(postComment);
    }

    @Override
    @Transactional
    public boolean remove(PostCommentRequest postCommentRequest) {
        Long id = postCommentRequest.getId();
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(PostCommentRequest postCommentRequest) {
        postCommentRequest.setUpdatedTime(System.currentTimeMillis());
        PostComment postComment = BeanUtil.copyProperties(postCommentRequest, PostComment.class);
        return this.updateById(postComment);
    }

    @Override
    public PostCommentRequest getById(PostCommentRequest postCommentRequest) {
        Long id = postCommentRequest.getId();
        PostComment postComment = this.getById(id);
        if (postComment != null) {
            return BeanUtil.copyProperties(postComment, PostCommentRequest.class);
        }
        return null;
    }

    @Override
    public List<PostCommentRequest> queryList(PostCommentRequest postCommentRequest) {
        QueryWrapper<PostComment> queryWrapper = new QueryWrapper<>();
        this.buildWrapper(queryWrapper, postCommentRequest);
        List<PostComment> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return BeanUtil.copyToList(list, PostCommentRequest.class);
    }

    @Override
    public Page queryPage(PostCommentRequest postCommentRequest) {
        Page<PostComment> page = new Page<>();
        QueryWrapper<PostComment> wrapper = new QueryWrapper<PostComment>();
        buildWrapper(wrapper, postCommentRequest);
        Page<PostComment> pageResult = this.page(page, wrapper);
        return pageResult;
    }
    private void buildWrapper(QueryWrapper<PostComment> wrapper, PostCommentRequest param) {
        log.info("buildPageWrapper param :{}", param);
        Long id = param.getId();
        if (id != null) {
            wrapper.eq("id", id);
        }
        Long postId = param.getPostId();
        if (postId != null) {
            wrapper.eq("post_id", postId);
        }
        Long commentedUserId = param.getCommentedUserId();
        if (commentedUserId!=null) {
            wrapper.eq("commented_user_id", commentedUserId);
        }
        Long commentUserId = param.getCommentUserId();
        if (commentUserId != null) {
            wrapper.eq("comment_user_id", commentUserId);
        }
        String commentUserName = param.getCommentUserName();
        if (StringUtils.isNotBlank(commentUserName)) {
            wrapper.eq("comment_user_name", commentUserName);
        }
        String commentHeadImage = param.getCommentHeadImage();
        if (StringUtils.isNotBlank(commentHeadImage)) {
            wrapper.eq("comment_head_image", commentHeadImage);
        }
        String commentContent = param.getCommentContent();
        if (StringUtils.isNotBlank(commentContent)) {
            wrapper.eq("comment_content", commentContent);
        }
        Integer commentLikeCounts = param.getCommentLikeCounts();
        if (commentLikeCounts != null) {
            wrapper.eq("comment_like_counts", commentLikeCounts);
        }
        Integer isRead = param.getIsRead();
        if (isRead != null) {
            wrapper.eq("is_read", isRead);
        }
        Integer isTakeDown = param.getIsTakeDown();
        if (isTakeDown != null) {
            wrapper.eq("is_take_down", isTakeDown);
        }
    }
}
